import router from '@src/router'
import {
  reloadSend
} from "@src/api/invoice"
import {
  _debounce
} from "@src/common/utils"
import {
  storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
Page({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  data: {
    popup: false,
    key: null,
    title: null,
    fapiaoStsZh: null,
    isShow: false,
    textMap: {
      1: '已出票',
      2: '已出票',
      3: '已失败', //开票失败
      4: '已冲红',
      5: '已冲红',
      6: '已出票'
    },
    colorMap: {
      1: '#4AB759',
      2: '#4AB759',
      3: '#F05357',
      4: '#F05357',
      5: '#F05357 '
    },
    goPath: {
      5: 'sp-mall/pages/order-detail/index', //商城
      17: 'sp-reservation/pages/reservation_order_detail/main', //预约
      7: 'sp-booking/pages/orderDetail/main', // 预定
      6: 'sp-catering/pages/orderdetails/main', // 点餐
      9: 'sp-member/pages/sendGiftDetail/main', //赠送会员
      2: 'sp-member/pages/balanceListDetail/main' // 会员充值 开卡 续费
    }
  },
  onPopup(e) {
    this.setData({
      popup: e.detail
    })
  },
  changeInvoic() {
    const timeFn = this.timeFn(this.data.key.finishTime)
    this.setData({
      popup: timeFn,
      isShow: !timeFn
    })
  },

  reloadSend: _debounce(
    function () {
      wx.showLoading({
        title: '重新发送中...',
      })
      reloadSend({
        fapiaoId: this.data.key.id
      }).then(res => {
        res.code === 0 && wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
      }).finally(()=>{
        wx.hideLoading()
      })
    }, 500
  ),

  goInvoiceView() {
    const {
      fapiaoJpgUrl
    } = this.data.key
    router.push({
      path: 'code-order/pages/invoiceView/main',
      success: (res) => {
        res.eventChannel.emit('url', {
          fapiaoJpgUrl
        })
      },
    })
  },
  goMyOrder(e) {
    const {
      goPath,
    } = this.data

    const {
      orderNo,
      shopId,
      orderType,
      receiveCode,
      fapiaoChargeDto
    } = this.data.key
    const path = goPath[orderType]

    if (orderType === 2) {
      router.push({
        path,
        success: (res) => {
          res.eventChannel.emit('item', fapiaoChargeDto)
        }
      })
    } else {
      router.push({
        path,
        params: {
          orderNo,
          shopId,
          receiveCode
        },
        success: (res) => {
          console.log('跳转成功', res);
        },
      })
    }

  },
  timeFn(d1) {
    let dateBegin = new Date(d1.replace(/-/g, "/"));
    let dateEnd = new Date();
    let dateDiff = (dateEnd.getTime() - dateBegin.getTime()) / 1000;
    const fixedTime = 30 * 12 * 60 * 60
    // true 可以修改 false 不可以修改
    return fixedTime - dateDiff >= 0

  },
  onOver(e) {
    this.setData({
      isShow: false
    })
  },
  close(e) {
    this.onOver()
  },
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('key', (key) => {
      this.setData({
        key
      })
    })
  },

});